package tabou.app;

import tabou.app.R;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ActivityOptions extends Activity {

	ControllerPartie controller;
	EditText txtNbPoints;
	EditText txtMinuteManche;
	EditText txtSecondeManche;
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.options);
        
        
        controller = ControllerPartie.getControllerPartie();
        txtNbPoints = (EditText)findViewById(R.id.txtNbPoints);
        txtMinuteManche = (EditText)findViewById(R.id.txtMinuteManche);        
        txtSecondeManche = (EditText)findViewById(R.id.txtSecondeManche);
        
        txtNbPoints.setText(controller.getNbPoint());
        txtMinuteManche.setText(controller.getMinuteManche());
        txtSecondeManche.setText(controller.getSecondeManche());
        
        Button btnEnregistrer = (Button)findViewById(R.id.btnEnregistrer);
        btnEnregistrer.setOnClickListener(new View.OnClickListener(){
	        public void onClick(View v)
	        {     
	        	String nbPts = txtNbPoints.getText().toString();
	        	controller.setNbPoint(nbPts);
	        	
	        	String minManche = txtMinuteManche.getText().toString();
	        	String secManche = txtSecondeManche.getText().toString();
	        	controller.setTpsManche(minManche, secManche);
	        	
//	        	Intent intJeu = new Intent(v.getContext(), ActivityAccueil.class);
//	            startActivityForResult(intJeu,0);
	        	finish();
	        }
		
	    }); 
        
        
        Button btnDefaut = (Button)findViewById(R.id.btnDefaut);
        btnDefaut.setOnClickListener(new View.OnClickListener(){
	        public void onClick(View v)
	        {   
	        	controller.setNbPoint("11");
	        	txtNbPoints.setText(controller.getNbPoint());
	        	
	        	controller.setTpsManche("00", "10");
	        	txtMinuteManche.setText(controller.getMinuteManche());
	            txtSecondeManche.setText(controller.getSecondeManche());
	            
	        	
	        }
		
	    }); 
        
		txtNbPoints.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
			public void afterTextChanged(Editable arg0) {
				boolean modified = false;
				if(txtNbPoints.length() != 0){
					modified = controller.checkNbPoint(txtNbPoints.getText().toString());
					if(modified)
					{
						txtNbPoints.setText(controller.getMaxValue());
					}
				}
			}
		}); 
			
		txtMinuteManche.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
			public void afterTextChanged(Editable arg0) {
				boolean modified = false;
				if(txtMinuteManche.length() != 0){
					modified = controller.checkMinuteManche(txtMinuteManche.getText().toString());
					if(modified)
					{
						txtMinuteManche.setText(controller.getMaxMinute());
					}
				}
			}
		}); 
		
		txtSecondeManche.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
			public void afterTextChanged(Editable arg0) {
				boolean modified = false;
				if(txtSecondeManche.length() != 0){
					modified = controller.checkSecondeManche(txtSecondeManche.getText().toString());
					if(modified)
					{
						txtSecondeManche.setText(controller.getMaxSeconde());
					}
				}
			}
		}); 
        
	}
        
}
